package com.kaizzi.data.jdl.helper;

import com.fasterxml.jackson.databind.JsonNode;
import com.kaizzi.data.common.been.TempBeen;
import com.kaizzi.data.common.utils.DateUtil;
import com.kaizzi.data.common.utils.JsonUtil;
import com.kaizzi.data.common.utils.LogicUtil;
import com.kaizzi.data.jdl.model.ExcelModel;
import com.kaizzi.data.jdl.service.MotionClocksService;
import lombok.Data;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * @author Kzen
 * @Description description
 * @date 2020/9/16
 */
@Component
public class PageData37 {

    @Resource
    private MotionClocksService motionClocksService;

    public String getData(ExcelModel model){
        List<String> yearWeeks = DateUtil.getYearWeeks();
//        List<String> yearWeeks = DateUtil.getMakeWeeks("2020-04-13");

        Page37 page37 = new Page37();
        List<TempPage37> tempPage37s = new ArrayList<>();
        yearWeeks.forEach(week -> {
            TempPage37 tempPage37 = new TempPage37();
            String[] split = week.split("#");
            long clockInTrend = motionClocksService.getClockInTrend(split[0], split[1]);

            tempPage37.setDate(DateUtil.getDateFormat37(split[0])+"-"+DateUtil.getDateFormat37(split[1]));
            tempPage37.setClockInCount(clockInTrend);
            tempPage37s.add(tempPage37);
        });

//        System.out.println(JsonUtil.serialize(tempPage37s));

//        List<TempPage37> fixedBeen = getFixedBeen();
//        fixedBeen.addAll(tempPage37s);
//        tempPage37s.addAll(fixedBeen);

//        tempPage37s.sort(Comparator.comparing(TempPage37::getDate));

        page37.setDataPeriod(DateUtil.getFirstDay2LastSunday());
        page37.setTempPage37s(tempPage37s);
        page37.setTitle(LogicUtil.getTitle(model,37));

        return JsonUtil.serialize(page37);
    }

    //写死数据 （数据在老库中恒定）
    private List<TempPage37> getFixedBeen(){
        String fixedStr = "[{\"date\":\"1.6-1.12\",\"clock_count\":80},{\"date\":\"1.13-1.19\",\"clock_count\":99},{\"date\":\"1.20-1.26\",\"clock_count\":53},{\"date\":\"1.27-2.2\",\"clock_count\":45},{\"date\":\"2.3-2.9\",\"clock_count\":43},{\"date\":\"2.10-2.16\",\"clock_count\":34},{\"date\":\"2.17-2.23\",\"clock_count\":71},{\"date\":\"2.24-3.1\",\"clock_count\":98},{\"date\":\"3.2-3.8\",\"clock_count\":152},{\"date\":\"3.9-3.15\",\"clock_count\":151},{\"date\":\"3.16-3.22\",\"clock_count\":124},{\"date\":\"3.23-3.29\",\"clock_count\":139},{\"date\":\"3.30-4.5\",\"clock_count\":135},{\"date\":\"4.6-4.12\",\"clock_count\":130}]";
        JsonNode jsonNode = JsonUtil.deserialize(fixedStr);
        List<TempPage37> beens = new ArrayList<>();
        for (JsonNode jsonNode1 : jsonNode) {
            TempPage37 tempBeen = new TempPage37();
//            getTransTime3
            tempBeen.setDate(jsonNode1.get("date").asText());
            tempBeen.setClockInCount(jsonNode1.get("clock_count").asLong());
            beens.add(tempBeen);
        }
        return beens;
    }

    @Data
    private static class Page37{
        private String title;
        private List<TempPage37> tempPage37s;
        private String dataPeriod;
    }

    @Data
    private static class TempPage37{
        private String date;
        private long clockInCount;
    }
}
